﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FineAdmin.Web.Areas.SysSet.Models;
using FineAdmin.Model;
using FineAdmin.Common;
using System.IO;
using FineAdmin.IService;
using Microsoft.Ajax.Utilities;

namespace FineAdmin.Web.Controllers
{
    public class HomeController : BaseController
    {


        public override ActionResult Index(int? id)
        {
            ViewBag.Account = Operator == null ? "" : Operator.Account;
            ViewBag.HeadIcon = Operator == null ? "" : Operator.HeadIcon;
            return View(new WebModel().GetWebInfo());
        }

        public ActionResult Main()
        {
            return View();
        }
        [HttpPost]
        public JsonResult AjaxMainData()
        {
            //商品总数
            var GoodsCount = MaterialGoodsService.CountByWhere("where 1=1");
            //淘口令调用次数
            var TKLOp = OpRecordService.GetByWhere("Where Name='淘口令' and MarkDate='" + DateTime.Now.Date + "'").FirstOrDefault();
            var TKLCount = TKLOp == null ? 0 : TKLOp.OpCount;
            //淘客API调用次数
            var ApiOpCount = OpRecordService.GetByWhere("Where GName='淘宝客API' and MarkDate='" + DateTime.Now.Date + "'").Sum(m => m.OpCount);
            //访问人次
            var FWRCOp = OpRecordService.GetByWhere("Where Name='访问人次' and MarkDate='" + DateTime.Now.Date + "'").FirstOrDefault();
            var FWRCOpWeek = OpRecordService.GetByWhere("Where Name='访问人次' and MarkDate >='" + DateTime.Now.Date.AddDays(-7) + "' and MarkDate <='" + DateTime.Now.Date + "'").ToList();
            var FWRCOpCount = FWRCOp == null ? 0 : FWRCOp.OpCount;
            //访问来源今日数据
            var FWLYOPToDay = OpRecordService.GetByWhere("Where GName='访问来源' and MarkDate='" + DateTime.Now.Date + "'").ToList();
            //访问来源周数据
            var FWLYOPWeek = OpRecordService.GetByWhere("Where GName='访问来源' and MarkDate >='" + DateTime.Now.Date.AddDays(-7) + "' and MarkDate <='" + DateTime.Now.Date + "'").ToList();

            //一周日期字符串
            var WeekStr = new List<string>();
            var WeekBefore7 = DateTime.Now.Date.AddDays(-7);
            for (int i = 0; i < 7; i++)
            {
                WeekBefore7 = WeekBefore7.AddDays(1);
                WeekStr.Add(WeekBefore7.ToShortDateString());
            }
            return Json(new
            {
                GoodsCount,
                TKLCount,
                ApiOpCount,
                FWRCOpCount,
                FWLYToday = FWLYOPToDay.Select(m => new { m.Name, m.OpCount }),
                FWLYWeek = FWLYOPWeek.GroupBy(m => new { m.MarkDate, m.Name }).Select(m => new { MarkDate = m.Key.MarkDate.Value.ToShortDateString(), m.Key.Name, OpCount = m.Sum(t => t.OpCount) }),
                WeekStr,
                FWRCOpWeek = FWRCOpWeek.Select(m => new { MarkDate = m.MarkDate.Value.ToShortDateString(), m.OpCount }),

            }, JsonRequestBehavior.DenyGet);

        }

        public JsonResult ExportFile()
        {
            UploadFile uploadFile = new UploadFile();
            try
            {
                var file = Request.Files[0];    //获取选中文件
                var filecombin = file.FileName.Split('.');
                if (file == null || string.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
                {
                    uploadFile.code = -1;
                    uploadFile.src = "";
                    uploadFile.msg = "上传出错!请检查文件名或文件内容";
                    return Json(uploadFile, JsonRequestBehavior.AllowGet);
                }
                //定义本地路径位置
                string localPath = Server.MapPath("~/Upload");
                string filePathName = string.Empty; //最终文件名
                filePathName = Common.Common.CreateNo() + "." + filecombin[1];
                //Upload不存在则创建文件夹
                if (!System.IO.Directory.Exists(localPath))
                {
                    System.IO.Directory.CreateDirectory(localPath);
                }
                file.SaveAs(Path.Combine(localPath, filePathName));  //保存图片
                uploadFile.code = 0;
                uploadFile.src = Path.Combine("/Upload/", filePathName);
                uploadFile.msg = "上传成功";
                return Json(uploadFile, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                uploadFile.code = -1;
                uploadFile.src = "";
                uploadFile.msg = "上传出错!程序异常";
                return Json(uploadFile, JsonRequestBehavior.AllowGet);
            }
        }

    }
}